<template>
  <div class="app-container">
    <el-table v-loading="loading" :data="associationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="id编号" align="center" prop="id" /> -->
      <el-table-column label="封面图" align="center">
        <template slot-scope="scope">
          <image-preview :src="scope.row.picUrl" width="100" height="100" />
        </template>
      </el-table-column>
      <el-table-column label="启用状态" align="center" prop="enabled">

        <template slot-scope="scope">
          <el-switch v-model="scope.row.enabled" :active-value="1" :inactive-value="0" active-color="#13ce66"
            inactive-color="#ff4949" @change="(value) => handleStatusChange(value, scope.row, scope.$index)" />
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center">

        <template slot-scope="scope">
          <span>{{ scope.row.createTime | timestampToDate }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">

        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['frist:association:edit']">修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改中美翻译协会对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="封面图" prop="picUrl">
          <oss :limit="1" v-model="form.picUrl" ossURL="oss/uploadTranslatePic"></oss>
        </el-form-item>
        <el-form-item label="状态" prop="enabled">
          <el-switch v-model="form.enabled" :active-value="1" :inactive-value="0" active-color="#13ce66"
            inactive-color="#ff4949" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAssociation, getAssociation, delAssociation, addAssociation, updateAssociation } from "@/api/frist/association";
import { timestampToDate } from '@/utils/time';

export default {
  name: "Association",
  filters: {
    timestampToDate
  },
  components: {
    oss: () => import("@/components/ImageUpload/oss.vue"),
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 中美翻译协会表格数据
      associationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        picUrl: null,
        enabled: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        picUrl: [
          { required: true, message: "图片路径不能为空", trigger: "blur" }
        ],
        enabled: [
          { required: true, message: "0, 不启用; 1,启用不能为空", trigger: "blur" }
        ]

      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询中美翻译协会列表 */
    getList() {
      this.loading = true;
      listAssociation(this.queryParams).then(response => {
        this.associationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    handleStatusChange(val, row, index) {
      updateAssociation({
        id: row.id,
        enabled: val
      }).then(association => {
        this.$modal.msgSuccess("修改成功");

        this.associationList[index].enabled = val;
      });
    },

    // 表单重置
    reset() {
      this.form = {
        id: null,
        picUrl: null,
        enabled: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加中美翻译协会";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      // getAssociation(id).then(response => {

      // });
      this.form = row;
      this.open = true;
      this.title = "修改中美翻译协会";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAssociation(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAssociation(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除中美翻译协会编号为"' + ids + '"的数据项？').then(function () {
        return delAssociation(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('frist/association/export', {
        ...this.queryParams,
        ids: this.ids.length? this.ids : undefined
      }, `association_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
